﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.DownLoads
{
    /// <summary>
    /// 下载类
    /// </summary>
    public class DownLoadHelper
    {
        /// <summary>
        /// 文件下载目录
        /// </summary>
        private static string _directory = "H:\\英语\\";


        public static void downfile(string url, string filename = "", string filePath = "H:\\temp\\")
        {
            try
            {
                System.IO.FileStream fs;
                var client = new HttpClient();
                string fileName = "";
                string fileExt = url.Substring(url.LastIndexOf(".")).Trim().ToLower();
                Random rnd = new Random();
                fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + rnd.Next(10, 99).ToString() + fileExt;

                //url = url + "?" + BuildParam(paramArray);
                client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");//添加自主驱动 很重要，没细研究，确实添加上就能下载文件
                client.DefaultRequestHeaders.Add("Accept", @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");//添加数据格式
                var bytes = client.GetByteArrayAsync(url).Result;

                if (File.Exists(filePath + "//" + fileName))
                {
                    File.Delete(filePath + "//" + fileName);
                }
                fs = new System.IO.FileStream(filePath + "//" + filename, System.IO.FileMode.CreateNew);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();


            }
            catch (Exception es)
            {

            }

        }





        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url">下载地址</param>
        /// <returns>文件名称</returns>
        public static string DownloadFile(string url, string name = "")
        {
            try
            {
                string fileName ;
                if (string.IsNullOrEmpty(name))
                {
                    fileName = CreateFileName(url);
                }
                else
                {
                    string fileExt = url.Substring(url.LastIndexOf(".")).Trim().ToLower();
                    fileName = name + fileExt;
                }

                if (!Directory.Exists(_directory))
                {
                    Directory.CreateDirectory(_directory);
                }
                WebClient client = new WebClient();
                client.DownloadFile(url, _directory + fileName);
                return fileName;
            }
            catch(Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// 创建文件名称
        /// </summary>
        public static string CreateFileName(string url)
        {
            string fileName = "";
            string fileExt = url.Substring(url.LastIndexOf(".")).Trim().ToLower();
            Random rnd = new Random();
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + rnd.Next(10, 99).ToString() + fileExt;
            return fileName;
        }


    }
}
